import { useManagerStoreWithOut } from '@/stores/manager'
import { ElMessage } from 'element-plus'
import router from '@/router'
import Request from '@/request'

/* 引入公共状态管理对象 */
const store = useManagerStoreWithOut()
/* 登录 */
function login(username, password, type) {
  const data = setData(username, password, type)
  localStorage.setItem('data', JSON.stringify(data))
  if (data.username === '' || data.password === '') {
    ElMessage({
      message: '用户名、密码不能为空',
      type: 'error'
    })
    return
  }
  const res = new Request(data)
  const result = res.login()
  result.then((res) => {
    if (res.data.code === 200) {
      ElMessage({
        message: res.data.msg,
        type: 'success'
      })
      router.push('/map')
    } else {
      ElMessage({
        message: res.data.msg,
        type: 'error'
      })
      if (data.type === 'common') {
        store.isLogin = false
      }
    }
  })
}

/* 退出登录 */
function logout() {
  const data = JSON.parse(localStorage.getItem('data'))
  const res = new Request(data)
  const result = res.logout(true)
  result.then((res) => {
    if (res.data.code === 200) {
      ElMessage({
        message: res.data.msg,
        type: 'info'
      })
      store.isLogin = true
      router.push('/login')
    } else {
      ElMessage({
        message: res.data.msg,
        type: 'error'
      })
    }
  })
}

/* 强制用户下线 */
function logoutUser(username, type) {
  const res = new Request({ username: username, password: '', type: type })
  const result = res.logout(false)
  result.then((res) => {
    if (res.data.code === 200) {
      ElMessage({
        message: res.data.msg,
        type: 'info'
      })
    } else {
      ElMessage({
        message: res.data.msg,
        type: 'error'
      })
    }
  })
}

/* 注册 */
function register(username, password, cpassword, type='common') {
  if (password !== cpassword) {
    ElMessage({
      message: '两次输入的密码不一致',
      type: 'error'
    })
    return
  }

  if (username === '' || password === '') {
    ElMessage({
      message: '用户名、密码不能为空',
      type: 'error'
    })
    return
  }
  const flag = check(password)
  if (!flag) {
    return
  }

  const data = setData(username, password, type)
  const res = new Request(data)
  const result = res.register()
  result.then((res) => {
    console.log(res.data)
    if (res.data.code === 200) {
      ElMessage({
        message: '操作成功',
        type: 'success'
      })

      store.isLogin = true
    } else {
      ElMessage({
        message: res.data.msg,
        type: 'error'
      })
    }
  })
}

/* 更新密码 */
function update(newPassword) {
  if (newPassword === '') {
    ElMessage({
      message: '新密码不能为空',
      type: 'error'
    })
    return
  }

  const flag = check(newPassword)
  if (!flag) {
    return
  }

  const data = JSON.parse(localStorage.getItem('data'))
  const res = new Request(data)
  const result = res.update(newPassword)
  result.then((res) => {
    if (res.data.code === 200) {
      ElMessage({
        message: res.data.msg,
        type: 'success'
      })
      store.isUpdate = false
      const newData = setData(data.username, newPassword, data.type)
      localStorage.setItem('data', JSON.stringify(newData))
    } else {
      ElMessage({
        message: res.data.msg,
        type: 'error'
      })
    }
  })
}

/* 切换登录注册 */
function toggleLogin() {
  store.isLogin = !store.isLogin
}

/* 切换更新密码 */
function toggleUpdate() {
  store.isUpdate = !store.isUpdate
}

/* 设置数据 */
function setData(username, password, type) {
  const data = {
    username: username,
    password: password,
    type: type
  }
  store.userData.setUsername(data.username)
  store.userData.setPassword(data.password)
  store.userData.setType(data.type)
  return data
}

/*验证密码是否符合要求*/
function check(password) {
  const reg = /^(?=.*[A-Za-z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,16}$/
  if (!reg.test(password)) {
    ElMessage({
      message: '密码至少包含一个大写字母、一个小写字母、一个数字和一个特殊字符，长度在8-16位之间',
      type: 'error',
      duration: 0,
      showClose: true
    })
    return false
  }
  return true
}

/* 获取用户信息 */
async function getUserInfo(type) {
  const data = JSON.parse(localStorage.getItem('data'))
  const res = new Request(data)
  const result = await res.getUser(type)
  return result
}

/* 修改用户信息 */
function changeUserInfo(form) {
  const data = JSON.parse(localStorage.getItem('data'))
  const res = new Request(data)
  res.change(form.username, form.type, form.password, form.id).then((res) => {
    console.log(res);
    if (res.data.code === 200) {
      ElMessage({
        message: res.data.msg,
        type: 'info'
      })
    } else {
      ElMessage({
        message: res.data.msg,
        type: 'error'
      })
    }
  })
}

/* 删除用户 */
function deleteUser(username, type) {
  const data = JSON.parse(localStorage.getItem('data'))
  const res = new Request(data)
  res.delete(username, type).then((res) => {
    if (res.data.code === 200) {
      ElMessage({
        message: res.data.msg,
        type: 'info'
      })
    } else {
      ElMessage({
        message: res.data.msg,
        type: 'error'
      })
    }
  })
}

export {
  login,
  logout,
  logoutUser,
  register,
  update,
  toggleLogin,
  toggleUpdate,
  getUserInfo,
  changeUserInfo,
  deleteUser
}
